package com.ysys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ysys.message.common.MessageUtil;
import com.ysys.message.resp.Article;
import com.ysys.message.resp.NewsMessage;

/**
 * 核心服务类
 * 
 * @author Administrator
 * 
 */
public class CoreService {
	/**
	 * 处理微信发来的请求
	 * 
	 * @param request
	 * @return
	 */
	public static String processRequest(HttpServletRequest request) {
		String respMessage = null;
		try {
			// xml请求解析
			Map<String, String> requestMap = MessageUtil.parseXml(request);

			// 发送方帐号（open_id）
			String fromUserName = requestMap.get("FromUserName");
			// 公众帐号
			String toUserName = requestMap.get("ToUserName");

			// 默认回复多条图文消息
		
			NewsMessage newsMessage = new NewsMessage();
			newsMessage.setToUserName(fromUserName);
			newsMessage.setFromUserName(toUserName);
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
			newsMessage.setFuncFlag(0);

			List<Article> articleList = new ArrayList<Article>();

			Article article1 = new Article();
			article1.setTitle("主题");
			article1.setDescription("主题");
			article1.setPicUrl("http://www.baidu.com");
			article1.setUrl("http://www.baidu.com");

			Article article2 = new Article();
			article2.setTitle("报价");
			article2.setDescription("报价");
			article2.setPicUrl("http://www.baidu.com");
			article2.setUrl("http://www.baidu.com");

			Article article3 = new Article();
			article3.setTitle("公司简介");
			article3.setDescription("公司简介");
			article3.setPicUrl("http://www.baidu.com");
			article3.setUrl("http://www.baidu.com");

			articleList.add(article1);
			articleList.add(article2);
			articleList.add(article3);
			newsMessage.setArticleCount(articleList.size());
			newsMessage.setArticles(articleList);
			respMessage = MessageUtil.newsMessageToXml(newsMessage);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return respMessage;
	}

	/**
	 * emoji表情转换(hex -> utf-16)
	 * 
	 * @param hexEmoji
	 * @return
	 */
	public static String emoji(int hexEmoji) {
		return String.valueOf(Character.toChars(hexEmoji));
	}
}
